package com.oa.modules.approvalsec1.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.oa.common.utils.AppUtils;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;
import com.oa.modules.approvalsec1.entity.NewsNoticeEntity;
import com.oa.modules.approvalsec1.service.NewsNoticeService;
import com.oa.modules.sys.controller.AbstractController;
import com.oa.modules.sys.entity.SysUserEntity;
import com.oa.modules.sys.service.SysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.oa.modules.approvalsec1.entity.ToDoImmediatelyEntity;
import com.oa.modules.approvalsec1.service.ToDoImmediatelyService;



/**
 * 立即办
 *
 * @author jiangwei
 * @email 785297226@qq.com
 * @date 2021-03-22 10:03:27
 */
@RestController
@RequestMapping("to/do/immediately")
@Api(tags = "立即办相关接口")
public class ToDoImmediatelyController extends AbstractController {
    @Autowired
    private ToDoImmediatelyService toDoImmediatelyService;

    @Autowired
    private NewsNoticeService newsNoticeService;

    @Autowired
    private SysUserRoleService userRoleService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = toDoImmediatelyService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") String id){
		ToDoImmediatelyEntity toDoImmediately = toDoImmediatelyService.getById(id);
        return R.ok().put("toDoImmediately", toDoImmediately);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody ToDoImmediatelyEntity toDoImmediately){
        String id = AppUtils.getUUID();
        toDoImmediately.setId(id);
        toDoImmediately.setCreateDate(new Date());
		toDoImmediatelyService.save(toDoImmediately);
        NewsNoticeEntity noticeEntity = new NewsNoticeEntity();
        noticeEntity.setId(AppUtils.getUUID());
        noticeEntity.setCreateDate(new Date());
        noticeEntity.setMatterId(id);
        noticeEntity.setMatterType("1");
        List<Long> userIds = userRoleService.queryRoleIdList(7L);
        for (int i = 0; i<userIds.size(); i ++){
            noticeEntity.setPushUserId(userIds.get(i).toString());
            newsNoticeService.save(noticeEntity);
            SysUserEntity user = sysUserService.getById(userIds.get(i));
            if(user != null && null != user.getWxOpenId()){
                String response = WxLoginController.noticePush(user.getWxOpenId(),"立即办","待分派", new Date(),new Date());
                System.err.println(response);
            }
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody ToDoImmediatelyEntity toDoImmediately){
        toDoImmediately.setUpdateDate(new Date());
		toDoImmediatelyService.updateById(toDoImmediately);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody String[] ids){
		toDoImmediatelyService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
